package com.yc.AdminUI;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FillLayout;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.wb.swt.SWTResourceManager;
import com.yc.dao.UserControlDao;
import com.yc.utils.StringUtil;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
 *  用户管理
 * @author UNO
 * 2021年6月24日下午7:03:14
 */
public class UserControl extends Composite {
	private Table table;
	private Text text;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public UserControl(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));
		setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(0, 66, 951, 459);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tableColumn = new TableColumn(table, SWT.CENTER);
		tableColumn.setWidth(95);
		tableColumn.setText("用户编号");

		TableColumn tableColumn_1 = new TableColumn(table, SWT.CENTER);
		tableColumn_1.setWidth(85);
		tableColumn_1.setText("用户姓名");

		TableColumn tableColumn_2 = new TableColumn(table, SWT.CENTER);
		tableColumn_2.setWidth(188);
		tableColumn_2.setText("邮箱");

		TableColumn tableColumn_3 = new TableColumn(table, SWT.CENTER);
		tableColumn_3.setWidth(216);
		tableColumn_3.setText("证件号码");

		TableColumn tableColumn_4 = new TableColumn(table, SWT.CENTER);
		tableColumn_4.setWidth(173);
		tableColumn_4.setText("手机号码");

		TableColumn tableColumn_5 = new TableColumn(table, SWT.CENTER);
		tableColumn_5.setWidth(100);
		tableColumn_5.setText("状态");

		TableCursor tableCursor = new TableCursor(table, SWT.NONE);

		Menu menu = new Menu(tableCursor);
		tableCursor.setMenu(menu);

		MenuItem menuItem = new MenuItem(menu, SWT.NONE);
		menuItem.setText("封号");

		MenuItem menuItem_1 = new MenuItem(menu, SWT.NONE);
		menuItem_1.setText("解封");

		Label label = new Label(composite, SWT.NONE);
		label.setText("用户姓名 ：");
		label.setBounds(104, 32, 76, 20);

		text = new Text(composite, SWT.BORDER | SWT.CENTER);
		text.setBounds(230, 29, 166, 26);

		Button button = new Button(composite, SWT.CENTER);
		button.setBounds(480, 24, 81, 36);
		button.setText("查询");
        
		menu.addMenuListener(new MenuAdapter() {
			@Override
			public void menuShown(MenuEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String status = String.valueOf(ti.getData("u_status"));
				if("1".equals(status)) {
					menuItem.setEnabled(false);
					menuItem_1.setEnabled(true);
				}else {
					menuItem.setEnabled(true);
					menuItem_1.setEnabled(false);
				}
			}
		});
		menuItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String account = ti.getText(1);//获取第二列的值，即用户名字
				UserControlDao usercontroldao = new UserControlDao();
				int result = usercontroldao.update(1 , account);
				if(result > 0) {
					finds();
				}
			}
		});
		menuItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				String account = ti.getText(1);//获取第二列的值，即用户名字
				UserControlDao usercontroldao = new UserControlDao();
				int result = usercontroldao.update(0,account);
				if(result > 0) {
					finds();
				}
			}
		});
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.removeAll();//移除表格中原有的数据
				UserControlDao usercontroldao = new UserControlDao();
				String account = text.getText().trim();//获取用户写入的名字
				if(StringUtil.checkNull(account)) {
					MessageDialog.openWarning(getShell(), "温馨提示", "用户的名字不能为空....");
					return;
				}
				List<Map<String, String>> list = usercontroldao.finds(account);//获取用户的相关信息
					if(list == null || list.isEmpty()) {
						MessageDialog.openError(getShell(), "错误提示", "此联系人不存在...");
						return;
					
				}
				finds();
			}
		});
		finds();
	}
	protected void finds() {
		UserControlDao usercontroldao = new UserControlDao();
		String account = text.getText().trim();//获取用户写入的名字
		List<Map<String, String>> list = usercontroldao.finds(account);//获取用户的相关信息
		//将所有商品类型信息显示在表格中
		table.removeAll();//移除表格中原有的数据
		if(list == null || list.isEmpty()) {
			return;
		}
		TableItem ti = null;//行对象
		for(Map<String, String> map : list) {
			ti = new TableItem(table, SWT.NONE);
			ti.setData("u_status", map.get("u_status"));
			if("0".equals(map.get("u_status"))) {
				ti.setText(new String[] {map.get("u_id"), map.get("u_name"), map.get("u_email"), map.get("u_cardid"), map.get("u_tel"), "正常"});
			}else {
				ti.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
				ti.setText(new String[] {map.get("u_id"), map.get("u_name"), map.get("u_email"), map.get("u_cardid"), map.get("u_tel"), "异常"});
			}
		}
	}
	@Override
	protected void checkSubclass() {
	}
}
